package com.example.kaogong.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.common.utils.SensitiveWordFilter;
import com.example.kaogong.sys.mapper.NoticeMapper;
import com.example.kaogong.sys.mapper.TeacherMapper;
import com.example.kaogong.sys.mapper.UserMapper;
import com.example.kaogong.sys.pojo.Notice;
import com.example.kaogong.sys.pojo.Teacher;
import com.example.kaogong.sys.pojo.User;
import com.example.kaogong.sys.pojo.dto.NoticeDto;
import com.example.kaogong.sys.pojo.vo.NoticeDetailVo;
import com.example.kaogong.sys.pojo.vo.NoticeVo;
import com.example.kaogong.sys.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 公告 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-26
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    /**
     * 分页获取资讯基本信息
     * @param current
     * @param size
     * @return
     */
    @Override
    public RespBean<Page<NoticeVo>> getAllBasicNotice(Long current, Long size) {
        Page<NoticeVo> noticeVoPage = noticeMapper.getAllBasicNotice(new Page<NoticeVo>(current, size), null);
        return RespBean.success(noticeVoPage);
    }

    /**
     * 根据资讯编号获取资讯详细信息
     * @param noticeId
     * @return
     */
    @Override
    public RespBean<NoticeDetailVo> getNoticeDetailById(Integer noticeId) {
        NoticeDetailVo noticeDetailVo = noticeMapper.getNoticeDetailById(noticeId);
        return RespBean.success(noticeDetailVo);
    }

    /**
     * 当前登录教师发布资讯
     * @param noticeDto
     * @param name
     * @return
     */
    @Override
    public RespBean publishNotice(NoticeDto noticeDto, String name) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", name));
        if (user == null || user.getRoleId() != 2)
            return RespBean.error();
        Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("uid", user.getUid()));
        if (teacher == null)
            return RespBean.error();

        Notice notice = new Notice();
        notice.setTitle(noticeDto.getTitle());
        notice.setContent(sensitiveWordFilter.filter(noticeDto.getContent()));
        notice.setPublishDate(LocalDate.now());
        notice.setTeacherId(teacher.getTeacherId());
        int insert = noticeMapper.insert(notice);
        if (insert < 1)
            return RespBean.error();
        return RespBean.success();
    }

    /**
     * 分页获取当前登录教师所发布的所有资讯基本信息
     * @param current
     * @param size
     * @param name
     * @return
     */
    @Override
    public RespBean<Page<NoticeVo>> getCurrentTeacherAllNotice(Long current, Long size, String name) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", name));
        if (user == null || user.getRoleId() != 2)
            return RespBean.error();
        Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("uid", user.getUid()));
        if (teacher == null)
            return RespBean.error();

        Page<NoticeVo> noticeVoPage = noticeMapper.getAllBasicNotice(new Page<NoticeVo>(current, size), teacher.getTeacherId());
        return RespBean.success(noticeVoPage);
    }

    /**
     * 根据资讯编号删除资讯
     * @param noticeId
     * @return
     */
    @Override
    public RespBean deleteNoticeById(Integer noticeId) {
        int delete = noticeMapper.delete(new QueryWrapper<Notice>().eq("notice_id", noticeId));
        if (delete < 1)
            return RespBean.error();
        return RespBean.success();
    }

    /**
     * 根据资讯编号修改资讯信息
     * @param noticeDto
     * @param noticeId
     * @return
     */
    @Override
    public RespBean updateNoticeById(NoticeDto noticeDto, Integer noticeId) {
        Notice notice = new Notice();
        notice.setContent(noticeDto.getContent());
        notice.setTitle(noticeDto.getTitle());
        int update = noticeMapper.update(notice, new UpdateWrapper<Notice>().eq("notice_id", noticeId));
        if (update < 1)
            return RespBean.error();
        return RespBean.success();
    }
}
